package com.yanqu.road.server.cmd.activity.lottery;

import com.yanqu.road.entity.activity.lottery.UserActivityLotteryData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityLotteryProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.lottery.ActivityLotteryConfig;
import com.yanqu.road.server.gameplayer.module.activity.lottery.ActivityLotteryModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.lottery.ActivityLotteryMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ACTIVITY_LOTTERY_SELECT_SKIN, desc = "更换皮肤")
public class LotteryChangeSkinCmd extends AbstractCommand {

    int activityId;
    int skinId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityLotteryProto.ActivityLotterySelectSkinReqMsg reqMsg = ActivityLotteryProto.ActivityLotterySelectSkinReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        skinId = reqMsg.getSkinId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityLotteryConfig config = ActivityLotteryMgr.getActivityLotteryConfig(activityId);
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        ActivityLotteryModule lotteryModule = player.getModule(ActivityLotteryModule.class);
        UserActivityLotteryData data = lotteryModule.getUserData(activityId);
        if (data == null) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        ret = lotteryModule.setSkin(activityId, skinId);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_ACTIVITY_LOTTERY_SELECT_SKIN, ActivityLotteryProto.ActivityLotterySelectSkinRespMsg.newBuilder().setRet(ret));
        }
    }
}
